package com.jshoperxms.controller.mall.backstage.nos;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.netease.cloud.services.nos.NosClient;
import com.netease.cloud.services.nos.model.Bucket;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.util.*;

/**
 * Created by sdywcd on 2018/9/10.
 * Des:
 */
@Controller
@RequestMapping("/mall/nos/bucket")
public class NosBucketsController extends BaseTController  implements IBaseCtrl<Bucket> {


    //数据形式
    private Map<String, Object> responsedata = new HashMap<>();

    private NosClient nosClient=this.getCli();


    @Override
    public ResponseEntity save(Bucket rq) throws ParseException {
        return null;
    }

    @Override
    public ResponseEntity find(Bucket rq) {
        return null;
    }

    @RequestMapping(value="/del",method= RequestMethod.POST)
    @Override
    public ResponseEntity del(Bucket rq) {
        return null;
    }

    @Override
    public ResponseEntity update(Bucket rq) throws ParseException {
        return null;
    }


    @Override
    public ResponseEntity findAll() {
        return null;
    }

    /**
     * 查询网易nos下的所有捅
     * @return
     */
    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        //检查是否有自定义搜索内容 1标示进行自定义搜索逻辑
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        findDefaultAll(searchModel,ordervalue,ordertype);
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {

    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int records=nosClient.listBuckets().size();
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        this.processPageList(nosClient.listBuckets());

    }

    @Override
    public void processPageList(List<Bucket> list) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for(Iterator<Bucket> it = list.iterator(); it.hasNext();){
            Bucket bean=it.next();
            Map<String,Object>cellMap=new HashMap<String, Object>();
            cellMap.put("id", bean.hashCode());
            cellMap.put("name", bean.getName());
            cellMap.put("bucketstatus",nosClient.getBucketAcl(bean.getName()));
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getCreationDate().getTime()));
            data.add(cellMap);
        }

        responsedata.put(TagWords.DATA, data);
    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
